package cn.sylinx.hbatis.ext.proxy.invoker;

import java.util.Map;

import cn.sylinx.hbatis.ext.mirage.spi.MirageServiceManager;
import cn.sylinx.hbatis.ext.parse.SqlParser;
import cn.sylinx.hbatis.ext.starter.util.DaoHelper;
import cn.sylinx.hbatis.kit.Tuple;

/**
 * 执行（插入、更新、ddl）
 * 
 * @author han
 *
 */
public class ExecuteCommandInvoker extends AbstractCommandInvoker {

	private String nativeSql;

	public ExecuteCommandInvoker(String truelyDatasource, String sql, Map<String, Object> params, String nativeSql) {
		super(truelyDatasource, sql, params);
		this.nativeSql = nativeSql;
	}

	@Override
	public Object invoke() {

		// 使用native sql
		boolean isNativeSql = nativeSql != null && !"".equals(nativeSql.trim());
		if (isNativeSql) {

			Tuple t = SqlParser.parseSql(nativeSql, params, null);
			String truelyNativeSql = t.getObject(0);
			Object[] truelyParams = t.getObject(1);

			return MirageServiceManager.getMirageService().useService(truelyDatasource).execute(truelyNativeSql,
					truelyParams);
		}

		return DaoHelper.create(truelyDatasource).execute(sql, params);
	}

}
